﻿using System;
using System.Data;
using System.Windows.Forms;

namespace docmgr.WinForm
{
    public partial class Login : Form
    {
        public int IsLogin = 0;
        docmgr.BLL.User userbll = new BLL.User();
        public Login()
        {
            
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));


            
        }

        private void Login_Load(object sender, EventArgs e)
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            BindDataWithPage(1);


            string currentPath = AppDomain.CurrentDomain.BaseDirectory;
            string exepath = currentPath + "conf.ini";
            IniFile ini = new IniFile(exepath);

            String autologin = ini.ReadInivalue("自动登录", "autologin");
            txtUserName.Text = ini.ReadInivalue("自动登录", "username");
            txtPWD.Text = ini.ReadInivalue("自动登录", "password");

            if (autologin.Equals("true"))
            {
                doLogin();
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            doLogin();
        }

        private void doLogin()
        {

           
            string userName = "";
            string passWord = "";
            userName =txtUserName.Text.ToString();
            passWord = Common.EncryptCode(txtPWD.Text.ToString());


            try
            {
                if (userName != "")
                {
                    /*if (SQLHelper.connString == null || SQLHelper.connString == "")
                    {
                        SQLHelper.getConnectionString();
                    }
                    string sql = "select * from dbo.[User] where userName = '" + userName + "' and password = '" + passWord + "'";
                    SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.connString, sql);*/

                    DataTable dt = userbll.Login(userName,passWord);
                    if (dt.Rows.Count==1)
                    {
                        IsLogin = 1;
                        Program.userName = userName;
                        Program.passWord = passWord;
                        Program._passWord = txtPWD.Text.ToString();
                        using (dt)
                        {
                            Common.curUser = dt;
                        }

                        if (Convert.ToInt32(Common.curUser.Rows[0]["PositionID"])!=1)
                        {
                            Common.DocType.Remove("收文");
                        }
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("用户名密码错误，请重新输入！");
                        txtPWD.Text = "";
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("用户名不能为空！");
                    return;
                }
            }
            catch (Exception timeEx)
            {
                MessageBox.Show("数据库连接超时，检查数据库连接！");
                Console.WriteLine(timeEx.StackTrace);
                return;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IsLogin = -1;
            this.Close();
        }


        private void BindDataWithPage(int Index)
        {
            /*allenPage1.PageIndex = Index;
            //winFormPager1.PageSize = 10;; ;
            DataSet ds = userbll.GetPage("id  ,name   ,username   ", "  ", allenPage1.PageIndex, allenPage1.PageSize);

            dataGridView1.DataSource = ds.Tables[0];

            //获取并设置总记录数
            allenPage1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);*/
        }

        private void allenPage1_PageIndexChanged(object sender, EventArgs e)
        {
            //BindDataWithPage(allenPage1.PageIndex);
        }

        private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                txtPWD.Focus();
            }
        }

        private void txtPWD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                doLogin();
            }
        }    
    }
}
